package
{
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.utils.getDefinitionByName;

	public class csSoundTrack
	{
		private var _container:MovieClip;
		private var _sound:Sound;
		private var _channel:SoundChannel;
		
		public function csSoundTrack(container:MovieClip, id:String, loops:int = 1)
		{
			_container = container;
			
			var cls:Class = getDefinitionByName(id) as Class;
			_sound = new cls();

			if(loops >= 0)
			{
				start(loops);
			}

			addToContainer();
		}
		
		private function addToContainer():void
		{
			if(_container != null)
			{
				var tracks:Array = _container[_attribute.ATTR_SOUND_TRACKS];
				if(tracks == null)
				{
					tracks = new Array();
					_container[_attribute.ATTR_SOUND_TRACKS] = tracks;
				}
				tracks.push(this);
			}
		}
		
		private function removeFromContainer():void
		{
			if(_container != null)
			{
				var tracks:Array = _container[_attribute.ATTR_SOUND_TRACKS];
				if(tracks != null)
				{
					var index:int = tracks.indexOf(this);
					if(index >= 0)
					{
						tracks.splice(index, 1);
					}
				}
			}
		}
		
		internal function setVolume(v:Number):void
		{
			if(_channel != null)
			{
				var globalVolume:Number = getGlobalVolume();
				_channel.soundTransform = new SoundTransform(v / 100 * globalVolume / 100, _channel.soundTransform.pan);
			}
		}
		
		internal function setPan(v:Number):void
		{
			if(_channel != null)
			{
				var globalPan:Number = getGlobalPan();
				_channel.soundTransform = new SoundTransform(_channel.soundTransform.volume, v / 100 * globalPan / 100);
			}
		}
		
		internal function start(loops:int):void
		{
			stop();

			try
			{
				_channel = _sound.play(0, loops, null);
				if(_channel != null)
				{
					_channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);

					function onSoundComplete(e:Event):void
					{
						removeFromContainer();
					}
				}
			}
			catch(e:Error)
			{
				
			}
		}
		
		public function stop():void
		{
			if(_channel != null)
			{
				_channel.stop();
				_channel = null;
			}
			
			removeFromContainer();
		}

		private function getGlobalVolume():Number
		{
			var volume:Number = 100;
			var node:DisplayObjectContainer = _container;
			while(node != null)
			{
				if(node.hasOwnProperty(_attribute.ATTR_SOUND_VOLUME))
				{
					volume *= node[_attribute.ATTR_SOUND_VOLUME] / 100;
				}
				node = node.parent;				
			}
			return volume;
		}
		
		private function getGlobalPan():Number
		{
			var volume:Number = 100;
			var node:DisplayObjectContainer = _container;
			while(node != null)
			{
				if(node.hasOwnProperty(_attribute.ATTR_SOUND_PAN))
				{
					volume *= node[_attribute.ATTR_SOUND_PAN] / 100;
				}
				node = node.parent;				
			}
			return volume;
		}
		

	}
}